<?php
namespace Wx\Controller;
use Think\Controller;
class VadminController extends CommonController {
	//书记搜索
	public function vadmin_ls(){
		$village_admin=M('village_admin');
		$arcli=array();
		$page=1;//第几页
		if($_GET['page'])$page=$_GET['page'];
		if($_GET['keyword']){
				$arcli=$village_admin
				->join('tv_user_info ON tv_user_info.u_id=tv_village_admin.user_id')->where("tv_user_info.name like '%".$_GET['keyword']."%'")->group('tv_village_admin.user_id')->page($page.',8')->order('tv_village_admin.id desc')->select(); // 查询第一页数据
				$this->assign('keyword',$_GET['keyword']);// 
			}else{
				$arcli=$village_admin->page($page.',8')->group('user_id')->order('id desc')->select(); // 查询第一页数据
			}
		//dump($arcli);return;
		$user=D('user');
		$user_info=D('user_info');
		$arc_diary=D('arc_diary');
		//用户信息
		foreach($arcli as $i=>$one){
			$getuser=$user->where("id=".$one['user_id'])->find();
			$getinfo=$user_info->where("u_id=".$one['user_id'])->find();
			//日志数
			$arcli[$i]['arc_num']=$arc_diary->where("u_id=".$one['user_id'])->count();
			$arcli[$i]=array_merge($arcli[$i],(array)$getuser,(array)$getinfo);
		}//end for
		
		//dump($arcli);return;
		if($_GET['type']){
			$this->ajaxReturn($arcli,'JSON');
			}else{
			$this->assign('arcli',$arcli);//
			$this->display('secretary');
			}
	}//end fun
	//第一书记详情页
	public function info(){
		$user=D('user');
		$user_info=D('user_info');
		$arc_diary=D('arc_diary');
		if(is_numeric($_GET['id'])){
			//书记资料
			$info=$user_info->where("u_id=".$_GET['id'])->find();
			//日志数量
			$info['diary_num']=$arc_diary->where("u_id=".$_GET['id'])->count();
			$this->assign('info',$info);//
			
			//日志列表******************************************
			$arc_diary=D('arc_diary');
			$arcli=array();
			$page=1;//第几页
			if($_GET['page'])$page=$_GET['page'];
			$arcli=$arc_diary->page($page.',8')->where("u_id=".$_GET['id'])->order('id desc')->select(); // 查询第一页数据
			//用户信息
			foreach($arcli as $i=>$one){
				$arcli[$i]['arc_id']=$one['id'];//文章id备份
				$arcli[$i]['arc_img']=$one['img'];//文章id备份
				$getuser=$user->where("id=".$one['u_id'])->find();
				$getinfo=$user_info->where("u_id=".$one['u_id'])->find();
				//提取文章中图片
				$imgarr=array();
				preg_match_all('/<img.*?src="(.*?)".*?>/is',$one['content'],$imgarr);
				$arcli[$i]['img_num']=count($imgarr[1]);//dump($imgarr[1]);
				$arcli[$i]['img_arr']=array_slice($imgarr[1],0,3);//只要前三个图片
				//评论统计
				$comments=D('comments');
				$arcli[$i]['com_num']=$comments->where("arc_type like 'diary' and arc_id=".$one['id'])->count();
				$arcli[$i]=array_merge($arcli[$i],(array)$getuser,(array)$getinfo);
			}//end for
			
			//dump($arcli);
			if($_GET['type']){
				$this->ajaxReturn($arcli,'JSON');
				}else{
				$this->assign('arcli',$arcli);//
				$this->display('secretaryDetail');
				}
			
			}//end if
			
	}//end fun
	//书记点赞
	public function va_supports(){
		$user_info=D('user_info');
		if(is_numeric($_GET['id'])){
			if($_GET['dowhat']=='add'){
				$user_info->where("id=".$_GET['id'])->setInc('supports'); // 阅读加1
				}else{
					$user_info->where("id=".$_GET['id'])->setDec('supports'); // 阅读减1
				}
		}
	}//end fun
	//书记管理页面
	public function index(){
		$village_admin=D("village_admin");
		$getone=array();
		if(session('uid'))$getone=$village_admin->where("user_id=".session('uid'))->find();
		if($getone){
			//书记信息
			$user_info=D('user_info');
			$getinfo=$user_info->where("u_id=".session('uid'))->find();
			if($getinfo){
				
				}else{
					//插入一条书记信息
					$data['u_id']=session('uid');
					$user_info->add($data);
				}//end if
			$this->assign('info',$getinfo);//
			$this->display('message');
			}else{
				$this->error('请您通知管理员绑定村庄！');
			}
		//dump(session('uid'));
		
	}//end fun
	//修改书记姓名
	public function up_name(){
		$user_info=D('user_info');
		if($_POST['name']){
			$data['name']=$_POST['name'];
			$alt=$user_info->where("u_id=".session('uid'))->save($data);
			if($alt){
				$this->success('修改成功', '/Wx/Vadmin/index/');
				}else{
					$this->error('修改失败！');
				}
			}else{
				$this->display('amend');
			}
		
	}//end fun
	//修改书记简历
	public function up_info(){
		$user_info=D('user_info');
		if($_POST['info']){
			$data['info']=$_POST['info'];
			$alt=$user_info->where("u_id=".session('uid'))->save($data);
			if($alt){
				$this->success('修改成功', '/Wx/Vadmin/index/');
				}else{
					$this->error('修改失败！');
				}
			}else{
				$getinfo=$user_info->where("u_id=".session('uid'))->find();
				$this->assign('info',$getinfo);
				$this->display('amend02');
			}
		
	}//end fun
	//管理自己的日志列表
	public function diary_ls(){
		$arc_diary=D('arc_diary');
		$comments=D('comments');
		$getall=$arc_diary->where("u_id=".session('uid'))->order('id desc')->select();
		foreach($getall as $i=>$one){
			$getall[$i]['com_num']=$comments->where("arc_type like 'diary' and arc_id=".$one['id'])->count();
		}//end for
		$this->assign('list',$getall);
		$this->display('journalMessage');
	}//end fun
	//书记删除自己的日志
	public function diary_del(){
		$arc_diary=D('arc_diary');
		$arc_diary->where("id=".$_GET['id'])->delete();
	}//end fun
	//添加日志
	public function diary_add(){
		$arc_diary=D('arc_diary');
		if(!session('uid'))$this->error('未登录！');
		if(session('uid')&&$_POST['dowhat']){
			//在登录状态且dowhat赋值，则启动添加
					//处理多余的逗号		
					$data['imgs']=preg_replace('/(,)+/i',',',$_POST['imgs']);
					$data['imgs']=rtrim($data['imgs'],',');//去尾
					$data['imgs']=ltrim($data['imgs'],',');//去头
			$data['u_id']=session('uid');
			$data['v_id']=$_POST['v_id'];
			$data['title']=$_POST['title'];
			$data['content']=$_POST['content'];
			$data['addtime']=time();
			$data['uptime']=time();
			$alt=$arc_diary->add($data);
			if($alt){
				$this->success('添加成功', '/Wx/Vadmin/diary_ls/');
				}else{
					$this->error('添加失败！');
				}
			}else{
				//展示绑定的村庄信息
				$village_admin=D('village_admin');
				$village=D('village');
				$vs=$village_admin->where("user_id=".session('uid'))->select();
				foreach($vs as $i=>$one){
					$getone=$village->where("id=".$one['v_id'])->find();
					$vs[$i]['title']=$getone['title'];
					}//end for
				$this->assign('vs',$vs);
				$this->display('addjournal');
			}
		
	}//end fun
	//***************************************************************************
	//管理自己的贤达列表
	public function celebrity_ls(){
		$arc_celebrity=D('arc_celebrity');
		$comments=D('comments');
		$getall=$arc_celebrity->where("u_id=".session('uid'))->order('id desc')->select();
		foreach($getall as $i=>$one){
			$getall[$i]['com_num']=$comments->where("arc_type like 'celebrity' and arc_id=".$one['id'])->count();
		}//end for
		$this->assign('list',$getall);
		$this->display('worthyMessage');
	}//end fun
	//书记删除自己的贤达 
	public function celebrity_del(){
		$arc_celebrity=D('arc_celebrity');
		$getone=$arc_celebrity->where("id=".$_GET['id'])->find();//dump($getone);
		//先删除图片
		if($getone['img']!=''){
			$file = './Public/Uploads/'.$getone['img'];
			if (!unlink($file))
			  {
			  echo ("删除文件失败 $file");
			  }
			else
			  {
			  echo ("成功删除 $file");
			  }
		}
		$arc_celebrity->where("id=".$_GET['id'])->delete();
	}//end fun
	//添加贤达
	public function celebrity_add(){
		$arc_celebrity=D('arc_celebrity');
		if(!session('uid'))$this->error('未登录！');
		if(session('uid')&&$_POST['dowhat']){
			//在登录状态且dowhat赋值，则启动添加
			//图片上传
			//单文件上传
			if($_FILES['img']["name"]!=''){
				$upload = new \Think\Upload();// 实例化上传类
				$upload->maxSize   =     3145728 ;// 设置附件上传大小
				$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
				$upload->rootPath  =      './Public/Uploads/'; // 设置附件上传根目录
				$upload->autoSub = false;	//关闭日期类型子目录自动生成
				$upload->savePath  =      'celebrity/'; // 设置附件上传（子）目录
				// 上传单个文件 
				$info   =   $upload->uploadOne($_FILES['img']);
				if(!$info) {// 上传错误提示错误信息
					$this->error($upload->getError());
				}else{// 上传成功 获取上传文件信息
					 $data['img']=$info['savepath'].$info['savename'];
				}
			}
			//图片上传end
					//处理多余的逗号	
					$data['imgs']=preg_replace('/(,)+/i',',',$_POST['imgs']);
					$data['imgs']=rtrim($data['imgs'],',');//去尾
					$data['imgs']=ltrim($data['imgs'],',');//去头
			$data['u_id']=session('uid');
			$data['v_id']=$_POST['v_id'];
			$data['title']=$_POST['title'];
			$data['content']=$_POST['content'];
			$data['addtime']=time();
			$data['uptime']=time();
			$alt=$arc_celebrity->add($data);
			if($alt){
				$this->success('添加成功', '/Wx/Vadmin/celebrity_ls/');
				}else{
					$this->error('添加失败！');
				}
			}else{
				//展示绑定的村庄信息
				$village_admin=D('village_admin');
				$village=D('village');
				$vs=$village_admin->where("user_id=".session('uid'))->select();
				foreach($vs as $i=>$one){
					$getone=$village->where("id=".$one['v_id'])->find();
					$vs[$i]['title']=$getone['title'];
					}//end for
				$this->assign('vs',$vs);
				$this->display('addworthy');
			}
		
	}//end fun
	//***************************************************************************
}